<?php
namespace backend\controllers;


use backend\models\MigrationPackageTransfer;
use backend\models\MigrationProject;
use backend\models\ProjectPreEvaluationMortgage;
use backend\models\ProjectPreEvaluationSealClone;
use backend\models\UserClone;
use yii;
use yii\filters\AccessControl;
use backend\models\PackageEase;
use backend\models\Package;
use backend\models\MigrationPackage;
use backend\models\PackageEaseTransfer;
use backend\models\PackageTransfer;
use backend\models\ProjectEase;
use backend\models\Project;

class PackageMigrationController extends yii\web\Controller
{
    public function behaviors()
    {
        return parent::behaviors();

//        return [
//            'access' => [
//                'class' => AccessControl::className(),
//                'rules' => [
//                    [
//                        'actions' => [],
//                        'allow' => true,
//                        'roles' => ['@'],
//                    ],
//                ],
//            ],
//        ];
    }

    public function actionPackage()
    {
        $migrationModel = new MigrationPackage();
        $latest_package_id = MigrationPackage::find()->orderBy("id DESC")->one()->original_package_id;
        if ($latest_package_id) {
            $map = "id>$latest_package_id";
        } else {
            $map = "";
        }

        $arr_package_ease = PackageEase::find()->where($map)->all();
        $packageModel = new Package();

        if (is_array($arr_package_ease) && count($arr_package_ease) > 0) {
            foreach ($arr_package_ease as $count => $package) {
                $clonePackageModel = clone $packageModel;
                $clonePackageModel->attributes = $package->getAttributes();
                $clonePackageModel->is_finished = 1;
//                $owner = UserClone::getNewId($package->owner);//获取新的用户id
//                if ($owner) {
//                    $clonePackageModel->owner = $owner;
//                } else {
//                    echo $package->owner . "无对应用户" . $package->id;
//                }

                if ($clonePackageModel->save()) {
                    $cloneMigrationModel = clone $migrationModel;
                    $cloneMigrationModel->original_package_id = $package->id;
                    $cloneMigrationModel->new_package_id = $clonePackageModel->id;

                    @$cloneMigrationModel->save();
                    echo 'Package ' . $package->id . ' migrate OK' . '<br>';
                } else {
                    echo 'Package ' . $package->id . " migrate error:" . serialize($clonePackageModel->getFirstErrors()) . '<br>';
                    Yii::info('Package' . $package->id . " migrate error:" . serialize($clonePackageModel->getFirstErrors()));
                }
                //每10条测试
//                if ($count == 9) {
//                    break;
//                }
            }

        } else {
            echo "No package record migrated";
        }
    }

    public function actionTransfer()
    {
        $migrationModel = new MigrationPackageTransfer();

        $latest_id = MigrationPackageTransfer::find()->orderBy("id DESC")->one()->original_transfer_id;
        if ($latest_id) {
            $map = "id>$latest_id";
        } else {
            $map = "";
        }

        $arr_transfer = PackageEaseTransfer::find()->where($map)->all();
        $transferModel = new PackageTransfer();
        if (is_array($arr_transfer) && count($arr_transfer) > 0) {
            foreach ($arr_transfer as $key => $item) {
                $cloneTransferModel = clone $transferModel;
                $cloneTransferModel->attributes = $item->getAttributes();
                $cloneTransferModel->package_id = MigrationPackage::getPackageId($item->package_id);//取新的package_id
                if ($cloneTransferModel->save()) {
                    $cloneMigrationModel = clone $migrationModel;
                    $cloneMigrationModel->original_transfer_id = $item->id;
                    $cloneMigrationModel->new_transfer_id = $cloneTransferModel->id;
                    @$cloneMigrationModel->save();
                    echo "Package_transfer " . $item->id . ' migrate OK <br>';
                } else {
                    echo "Package_transfer " . $item->id . ' migrate error' . serialize($cloneTransferModel->getFirstErrors()) . '<br>';
                    Yii::info("Package_transfer " . $item->id . ' migrate error' . serialize($cloneTransferModel->getFirstErrors()));
                }
                //测试
//                if ($key == 20) {
//                    break;
//                }
            }
        } else {
            echo "No package_transfer record migrated";
        }
    }

    public function actionProject()
    {
        $migrationModel = new MigrationProject();
        $latest_id = MigrationProject::find()->orderBy("id DESC")->one()->original_project_id;
        if ($latest_id) {
            $map = "id>$latest_id";
        } else {
            $map = "";
        }

        $arr_project = ProjectEase::find()->where($map)->all();
        $projectModel = new Project();
        $projectPreEvalMortgage = new ProjectPreEvaluationMortgage();
        $projectPreEvalSeal = new ProjectPreEvaluationSealClone();

        if (is_array($arr_project) && count($arr_project) > 0) {
            //1存续2吊销3注销4破产
            $arr_management_status = [1 => 4, 2 => 1, 3 => 2, 4 => 3];

            foreach ($arr_project as $key => $item) {
                $cloneProjectModel = clone $projectModel;
                $original_project_attributes = $item->getAttributes();
                $project_attributes = array_slice($original_project_attributes, 1, 12);
//                var_dump($project_attributes);die;
                $cloneProjectModel->attributes = $project_attributes;
                $cloneProjectModel->guarantor = $item->stakeholders;
                $cloneProjectModel->guarantor_status = $item->stakeholders_status;
                $cloneProjectModel->mortgage_details = $item->mortgage_detail;
                $cloneProjectModel->package_id = MigrationPackage::getPackageId($item->package_id);
                $cloneProjectModel->type = '';
                $cloneProjectModel->type2 = $item->category;
                $cloneProjectModel->management_status = $arr_management_status[$item->management_status];
                if ($cloneProjectModel->save()) {
                    $cloneMigrationModel = clone $migrationModel;
                    $cloneMigrationModel->original_project_id = $item->id;
                    $cloneMigrationModel->new_project_id = $cloneProjectModel->id;
                    @$cloneMigrationModel->save();

                    $cloneMortgageModel = clone $projectPreEvalMortgage;
                    $mortgage_values = [
                        'project_id' => $cloneProjectModel->id,
                        'mortgagor' => $item->stakeholders,
                        'mortgage_principal' => $item->mortgage_principal,
                        'mortgage_interest' => $item->mortgage_interest,
                        'guarantor' => $item->stakeholders,
                    ];
                    $cloneMortgageModel->attributes = $mortgage_values;
                    if ($cloneMortgageModel->save()) {
                        $cloneSealModel = clone $projectPreEvalSeal;
                        $seal_values = [
                            'mortgage_id' => $cloneMortgageModel->id,
                            'mortgage_owner' => $item->mortgage_owner,
                            'mortgage_type' => $item->mortgage_type,
                            'other_warrants_code' => $item->other_warrants_code,
                            'mortgage_address' => $item->mortgage_address,
                            'mortgage_building_area' => $item->mortgage_building_area,
                            'mortgage_land_area' => $item->mortgage_land_area,
                        ];
                        $cloneSealModel->attributes = $seal_values;
                        if ($cloneSealModel->save()) {
                            //
                        } else {
                            echo "Project " . $item->id . ' migrate project seal error' . serialize($cloneSealModel->getFirstErrors()) . '<br>';
                        }
                    } else {
                        echo "Project " . $item->id . ' migrate mortgage error' . serialize($cloneMortgageModel->getFirstErrors()) . '<br>';
                    }

                    echo "Project " . $item->id . ' migrate OK <br>';
                } else {
                    echo "Project " . $item->id . ' migrate error' . serialize($cloneProjectModel->getFirstErrors()) . '<br>';
                    Yii::info("Project " . $item->id . ' migrate error' . serialize($cloneProjectModel->getFirstErrors()));
                }

                //测试
//                if ($key == 10) {
//                    break;
//                }
            }
        } else {
            echo "No project record migrated";
        }
    }
}